-- coding: utf-8 --

Author: methylDragon (methylDragon.com)

"Raa."

Setting Up


In [7]:
#NLP
from pattern.web import Twitter
from textblob import TextBlob
import nltk.data 
from nltk.tokenize import word_tokenize, sent_tokenize

#NLTK RESOURCE DOWNLOADING
try:
    nltk.data.find('tokenizers/punkt')
except LookupError:
        nltk.download('punkt')

#PARSER
from newspaper import Article
import newspaper

#set tokenizer model
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')

Declare URL, Download, and Parse Article


In [39]:
#list, download, and parse article
a = Article("http://www.theindependent.sg/the-rule-of-law-has-been-mercilessly-mocked-and-denigrated-chee-soon-juan-on-pe2017/", language='en') # Set language as english
a.download()
a.parse()
a.nlp()

Analyze Article


In [40]:
#Quality of life bold delimiters
b = "\033[1m"
endb = "\033[0;0m"

#START OF ARTICLE
print(b + "START OF ARTICLE - START OF ARTICLE - START OF ARTICLE - START OF ARTICLE - START OF ARTICLE" + endb)

#Overall sentiment trackers
count = 0
overallScore = [0.0,0.0]

#Print meta-data
print("\n-----\n" + b + "METADATA" + endb + "\n-----")
print(b + "Title: " + endb, end="")
print(a.title)
print(b + "Author(s): " + endb, end="")
print(a.authors)
print(b + "Keywords: " + endb, end="")
print(a.keywords)
print(b + "Date: " + endb, end="")
print(a.publish_date)
print(b + "Top Image: " + endb, end="")
print(a.top_image)
print(b + "Videos: " + endb, end="")
print(a.movies)

#Print summary
print("\n-----\n" + b + "SUMMARY" + endb + "\n-----")
print(a.summary)

#Split article into sentences
print("\n-----\n" + b + "ANALYSIS" + endb + "\n-----")
for index, token in enumerate(tokenizer.tokenize(a.text)):
    analysis = TextBlob(tokenizer.tokenize(a.text)[index])
    # analysis.correct() #Correct mispelt words !!! IF YOU ACTIVATE THIS IT'LL BE SLOW
    print(tokenizer.tokenize(a.text)[index] + b) 
    #and for each sentence, analyze sentiment
    print(analysis.sentiment)
    #Prep overall analysis tracker, IGNORE if parameters are [0.0, 0.0] for sentence
    if analysis.sentiment.polarity != 0.0 and analysis.sentiment.subjectivity != 0.0:
        count += 1
        overallScore[0] += analysis.sentiment.polarity
        overallScore[1] += analysis.sentiment.subjectivity
    print(endb + "-----")

#Guarding against divisions by 0
if count == 0:
    count = 1
    
#Print overall sentiment
print("\n-----\n" + b + "OVERALL SENTIMENT" + endb + "\n-----")
#print(TextBlob(a.text).sentiment)
print(b + "Polarity: " + endb, end="")
print(overallScore[0]/count, end="   |   ")
print(b + "Subjectivity: " + endb, end="")
print(overallScore[1]/count, end="")
print(endb + "\n")
print(b + "END OF ARTICLE - END OF ARTICLE - END OF ARTICLE - END OF ARTICLE - END OF ARTICLE" + endb)


START OF ARTICLE - START OF ARTICLE - START OF ARTICLE - START OF ARTICLE - START OF ARTICLE

-----
METADATA
-----
Title: “The rule of law has been mercilessly mocked and denigrated” – Chee Soon Juan on PE2017
Author(s): ['Jewel Philemon']
Keywords: ['revised', 'pe2017', 'law', 'juan', 'walkover', 'singapore', 'yacob', 'party', 'chee', 'presidential', 'system', 'election', 'mercilessly', 'soon', 'pap', 'mocked', 'denigrated', 'rule']
Date: None
Top Image: http://media.theindependent.sg/wp-content/uploads/2017/08/cheehali.jpg
Videos: []

-----
SUMMARY
-----
The PAP had changed the rules and revised the criteria of the Elected Presidency to pave the way for Ms Halimah Yacob to assume the office.
The rule of law has been mercilessly mocked and denigrated.
That the goal is detrimental to the interests and progress of Singapore is of secondary concern to the party.
“It is bad enough that the PAP has manipulated the system to get one if its own to become the president.
“The SDP protests this outcome in the strongest manner possible.”The walkover of the Presidential Election comes as no surprise.

-----
ANALYSIS
-----
Opposition politician Chee Soon Juan harshly criticised the outcome of the Presidential Election 2017 that was released this week, that former Speaker of Parliament Halimah Yacob would be the next head of state via walkover since the two other hopefuls do not qualify for the presidential race since they do not meet revised eligibility requirements.
Sentiment(polarity=-0.08125, subjectivity=0.26875)
-----
Republishing his party, the Singapore Democratic Party’s (SDP) official statement on his personal Facebook page, Dr Chee indicated that the ruling People’s Action Party has “manipulated the system” to get one of its own into power, and that the PAP should be condemned for its “contempt” of the constitution:

“The walkover of the Presidential Election comes as no surprise.
Sentiment(polarity=0.2333333333333333, subjectivity=0.4666666666666666)
-----
The PAP had changed the rules and revised the criteria of the Elected Presidency to pave the way for Ms Halimah Yacob to assume the office.
Sentiment(polarity=0.0, subjectivity=0.0)
-----
“Nevertheless, this is a sad day for Singapore.
Sentiment(polarity=-0.5, subjectivity=1.0)
-----
The rule of law has been mercilessly mocked and denigrated.
Sentiment(polarity=-0.7, subjectivity=1.0)
-----
The contempt the PAP has shown for our constitution and our flag which symbolises the ideals of democracy, unity and progress must be roundly condemned.
Sentiment(polarity=-0.2, subjectivity=0.4)
-----
“By doing what it has done, the PAP has shown yet again that its only goal is to strengthen its grip on power.
Sentiment(polarity=0.0, subjectivity=1.0)
-----
That the goal is detrimental to the interests and progress of Singapore is of secondary concern to the party.
Sentiment(polarity=-0.3, subjectivity=0.3)
-----
“It is bad enough that the PAP has manipulated the system to get one if its own to become the president.
Sentiment(polarity=-0.03333333333333329, subjectivity=0.7222222222222222)
-----
That it has dangerously played the race card and divided the people to achieve this must be of grave concern to all Singaporeans.
Sentiment(polarity=-0.6, subjectivity=0.9)
-----
“The SDP protests this outcome in the strongest manner possible.”

The walkover of the Presidential Election comes as no surprise.
Sentiment(polarity=0.0, subjectivity=1.0)
-----
The PAP had changed the rules and revised the criteria… Posted by Chee Soon Juan 徐顺全 on Monday, 11 September 2017

Halimah Yacob will be sworn in as the eighth President of Singapore tomorrow at 6pm at the Istana.
Sentiment(polarity=0.0, subjectivity=0.0)
-----

-----
OVERALL SENTIMENT
-----
Polarity: -0.27265625   |   Subjectivity: 0.6322048611111111

END OF ARTICLE - END OF ARTICLE - END OF ARTICLE - END OF ARTICLE - END OF ARTICLE

In [41]:
teehee = 0.13123
print("{0:.2f}".format(teehee))


0.13

In [ ]: